import java.util.PriorityQueue;

public class KthLargest {
    //第k大元素当存储k个元素时使用小根堆栈顶元素为第k大元素
    PriorityQueue<Integer> priorityQueue;
    int temp;
    public KthLargest(int k, int[] nums) {
        temp=k;
        priorityQueue=new PriorityQueue();
        for(int i:nums){
            priorityQueue.add(i);
            if(priorityQueue.size()>temp)priorityQueue.poll();
        }
    }
    public int add(int val) {
        priorityQueue.add(val);
        if(priorityQueue.size()>temp)priorityQueue.poll();
        return  priorityQueue.peek();
    }
}
